package com.tich.program.listener;

import cn.hutool.core.bean.BeanUtil;
import com.tich.constant.RabbitMqConstant;
import com.tich.program.domain.entity.ProgramTicket;
import com.tich.program.domain.entity.ProgramTicketDeleted;
import com.tich.program.mapper.ProgramDeletedMapper;
import com.tich.program.mapper.ProgramTicketDeletedMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ProgramTicketListener {

    @Autowired
    private ProgramTicketDeletedMapper programTicketDeletedMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = RabbitMqConstant.PROGRAM_TICKET_QUEUE),
            exchange = @Exchange(name = RabbitMqConstant.PROGRAM_DIRECT, type = ExchangeTypes.DIRECT),
            key = {RabbitMqConstant.PROGRAM_TICKET_DELETE_KEY}
    ))
    public void listenProgramTicketDelete(ProgramTicket programTicket){
        log.info("将已删除的节目门票添加到删除表中：{}", programTicket);
        ProgramTicketDeleted programTicketDeleted = BeanUtil.copyProperties(programTicket, ProgramTicketDeleted.class);
        programTicketDeletedMapper.insert(programTicketDeleted);
    }

}
